var Pool = require('../Tools/TestCenterPool').pool;
var _ = require('lodash');
var moment = require('moment');

class TestRecordLogModel {
    async insert(user, testRecordId, content, backReason) {
        try {
            let sql = `INSERT test_record_log (record_id,user_id,user_name,real_name,role_id,role_name,content, back_reason) VALUES 
                (${testRecordId},${user.id},'${user.userName}','${user.realName}',
                ${user.roleId},'${user.roleName}', '${content}', '${backReason}')`;
            let result = await Pool.queryAsync(sql);
            return result;
        } catch (error) {
            throw error;
        }
    }

    async queryRecordLogByTestRecordId(testRecordId) {
        try {
            let sql = `SELECT * FROM test_record_log WHERE record_id=${testRecordId}`;
            let recordLogList = await Pool.queryAsync(sql);
            recordLogList = recordLogList.map((r)=>{
                let recordLogCreateTime = moment(r.create_time).format('YYYY-MM-DD HH:mm:ss'); 
                return {
                    id: r.id,
                   operatorId: r.user_id,
                   operatorUserName: r.user_name,
                   operatorRealName: r.real_name,
                   operatorRoleId: r.role_id,
                   operatorRoleName: r.role_name,
                   recordLogContent: r.content,
                   testBackReason: r.back_reason,
                   recordLogCreateTime: recordLogCreateTime
                }
            });
            return recordLogList;
        } catch (error) {
            throw error;
        }
    }
}

module.exports = new TestRecordLogModel();

let tml = new TestRecordLogModel();
// tml.getRecordLogByTestRecordId(1).then((data) => {
//     console.log(data)
// })
// tml.insert({ id: 5, userName:'aaa', realName:'bbb', roleId:'2', roleName:'开发'}, 1, '提交测试', 'wergqg' ).then((data) => {
//     console.log(data);
// });